//
//  MineInfoEditViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/17.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "MineInfoEditViewController.h"

@interface MineInfoEditViewController ()<UITextFieldDelegate>
{
    UITextField *infoTextField;
    UILabel *showNumberLabel;
    NSInteger maxLimit;
}
@end

@implementation MineInfoEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    infoTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(30*Scale, 10*Scale, WIDTH-60*Scale, PSCellHeight) placeholder:@"请填写" secureTextEntry:NO font:Font_15 padding:YES];
    infoTextField.delegate = self;
    [self.view addSubview:infoTextField];
    [infoTextField borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    if (self.text && self.text.length) {
        infoTextField.text = self.text;
    }
    
    maxLimit = 20;
    BOOL hidden = YES;
    switch (self.type) {
        case InfoEditTypeOfName: {
            self.navigationItem.title = @"编辑姓名";
            hidden = NO;
            maxLimit = 8;
        } break;
        case InfoEditTypeOfNickName: {
            self.navigationItem.title = @"编辑昵称花名";
            hidden = NO;
            maxLimit = 15;
        } break;
        case InfoEditTypeOfPhoneNumber: {
            self.navigationItem.title = @"编辑手机号";
            infoTextField.keyboardType = UIKeyboardTypeNumberPad;
            hidden = YES;
        } break;
        case InfoEditTypeOfEmail: {
            self.navigationItem.title = @"编辑邮箱";
            infoTextField.keyboardType = UIKeyboardTypeEmailAddress;
            hidden = YES;
        } break;
        case InfoEditTypeOfDepartment: {
            self.navigationItem.title = @"我的部门";
            hidden = YES;
        } break;
        case InfoEditTypeOfPosition: {
            self.navigationItem.title = @"职位名称";
            hidden = YES;
        } break;
        case InfoEditTypeOfCompanyName: {
            self.navigationItem.title = @"我的公司";
            hidden = YES;
        } break;
        case InfoEditTypeOfCompanyNameForShort: {
            self.navigationItem.title = @"公司简称";
            hidden = YES;
        } break;
        case InfoEditTypeOfCompanyLandline: {
            self.navigationItem.title = @"公司座机";
            hidden = YES;
        } break;
        case InfoEditTypeOfCompanyAddress: {
            self.navigationItem.title = @"公司地址";
            hidden = YES;
        } break;
        case InfoEditTypeOfCompanyWebURL: {
            self.navigationItem.title = @"公司官网";
            hidden = YES;
        } break;
        case InfoEditTypeOfCompanyNotes: {
            self.navigationItem.title = @"备注";
            hidden = YES;
        } break;
        default:
            break;
    }
    
    showNumberLabel = [UIToolsControl initLabelWithFrame:CGRectMake(0, EndY(infoTextField), WIDTH-30*Scale, 50*Scale) text:[NSString stringWithFormat:@"0/%ld", (long)maxLimit] textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_13];
    showNumberLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:showNumberLabel];
    showNumberLabel.hidden = hidden;
}
- (void)okBtnTap {
    if (infoTextField.text) {
        switch (self.type) {
            case InfoEditTypeOfName:
            case InfoEditTypeOfNickName: {
                if (infoTextField.text.length>maxLimit) {
                    SVPHUDError(@"您输入的过长，请重新编辑");
                    [infoTextField becomeFirstResponder];
                } else {
                    [self popVC];
                }
            } break;
            case InfoEditTypeOfPhoneNumber: {
                if ([UIToolsControl validatePhoneNumber:infoTextField.text]) {
                    [self popVC];
                } else {
                    SVPHUDError(PhoneNumberOKAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfEmail: {
                if ([UIToolsControl validateEmail:infoTextField.text]) {
                    [self popVC];
                } else {
                    SVPHUDError(EmailOKAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfDepartment: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(DepartmentAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfPosition: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(PositionAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfCompanyName: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(CompanyAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfCompanyNameForShort: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(CompanyNameForShortAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfCompanyLandline: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(CompanyLandlineAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfCompanyAddress: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(CompanyAddressAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfCompanyWebURL: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(CompanyWebURLAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfCompanyResponsibility: {
                if (infoTextField.text.length) {
                    [self popVC];
                } else {
                    SVPHUDError(CompanyResponsibilityAlertText);
                    [infoTextField becomeFirstResponder];
                }
            } break;
            case InfoEditTypeOfCompanyNotes: {
                [self popVC];
            } break;
            default:
                break;
        }
    }
}
- (void)popVC {
    if (self.delegate && [self.delegate respondsToSelector:@selector(mineInfoEditViewController:type:)]) {
        [self.delegate mineInfoEditViewController:infoTextField.text type:self.type];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSMutableString *mutableString = [NSMutableString stringWithString:textField.text];
    [mutableString replaceCharactersInRange:range withString:string];
    if (mutableString.length) {
        showNumberLabel.text = [NSString stringWithFormat:@"%lu/%ld", (unsigned long)mutableString.length, (long)maxLimit];
    } else {
        showNumberLabel.text = [NSString stringWithFormat:@"0/%ld", (long)maxLimit];
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (infoTextField.isFirstResponder) {
        [infoTextField resignFirstResponder];
    }
}

@end
